package com.laikan.reader.core.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.laikan.library.utils.SPUtil;
import com.laikan.reader.core.LaikanApplication;
import com.laikan.reader.core.LaikanBaseAdapter;
import com.laikan.reader.R;
import com.laikan.reader.database.Book;
import com.laikan.reader.core.view.GridItem;
import com.laikan.reader.core.view.SimpleTagImageView;
import com.socks.library.KLog;

import java.util.ArrayList;
import java.util.List;

public class MyBookGridDelAdapter extends LaikanBaseAdapter<Book> {

    private List bookIds;
    public MyBookGridDelAdapter(Context context, ArrayList<Book> datas) {
        super(context, datas);
    }
    public void setSelectBook(List bookIds){
        this.bookIds = bookIds;
        this.notifyDataSetChanged();
    }
    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        GridItem gridItem = null;
        ViewHolder holder = null;
        if (convertView == null || convertView instanceof ImageView) {
            gridItem = new GridItem(mContext);
//            gridItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            holder = new ViewHolder();
            convertView = gridItem;
            holder.book_pic = (SimpleTagImageView)convertView.findViewById(R.id.book_pic);
            holder.select = (ImageView)convertView.findViewById(R.id.select);
            holder.book_name = (TextView)convertView.findViewById(R.id.book_name);
            holder.tv_read_record = (TextView)convertView.findViewById(R.id.tv_read_record);
            holder.selected = (ImageView)convertView.findViewById(R.id.selected);
            holder.shadow = (View)convertView.findViewById(R.id.shadow);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder)convertView.getTag();
        }

        if (datas.size() > 0) {
            KLog.e("booId: "+bookIds.get(0)+" datas.get(position).getId() "+datas.get(position).getId());
            if(bookIds.contains(datas.get(position).getId())){
                holder.selected.setVisibility(View.VISIBLE);
                holder.shadow.setBackgroundResource(R.drawable.tv_book_selected_shadow_bg);
            } else {
            // 显示未选图标
            holder.select.setVisibility(View.VISIBLE);
            holder.shadow.setBackgroundResource(R.drawable.tv_book_un_select_shadow_bg);
            }
            // 显示书籍封面
            holder.book_pic.setImageURI(Uri.parse(datas.get(position).getIcon()));
            // 设置封面阴影
//            holder.book_pic.setBackgroundResource(R.drawable.lk_mybook_shadow);
            // 设置显示当前阅读进度
            StringBuilder bookName = new StringBuilder(datas.get(position).getName());
            String book_id = datas.get(position).getId();
            int chapCount = SPUtil.getInt("chap_count_" + book_id, 0);
            int chapPos = SPUtil.getInt("chap_pos_" + book_id, 1);

            if (bookName.length() >= 7) {
                bookName = bookName.insert(6, "\r\n");
            }
            holder.book_name.setText(bookName);
            if (chapCount != 0) {
                holder.tv_read_record.setText(chapPos + "/" + chapCount);
            }

            // 设置书籍标签 文字
            Book b = datas.get(position);
            if (SPUtil.getBoolean("isUpdata_" + b.getId(), false) && !"完本".equals(b.getStatus())) {
                // 更新
                holder.book_pic.setTagWidth(15);
                holder.book_pic.setTag("更新");
            }

            // 设置书籍标签 背景色
            setTagColor(holder.book_pic, (String)holder.book_pic.getTag());
        }
        return convertView;
    }

    private void setTagColor(SimpleTagImageView pic, String tag) {
        if ("更新".equals(tag)) {

            pic.setTagBackgroundColor(LaikanApplication.applicationContext.getResources().getColor(R.color.C_88BB00));
        }else if ("限免".equals(tag)) {
//            pic.setBackgroundColor(mContext.getResources().getColor(R.color.C_3399FF));
        }else if ("一折".equals(tag)) {
//            pic.setBackgroundColor(mContext.getResources().getColor(R.color.C_EE0022));
        }
    }

    private class ViewHolder {

        private SimpleTagImageView book_pic;
        private ImageView select;
        private TextView book_name;
        private TextView tv_read_record;
        private ImageView selected;
        private View shadow;
    }

}
